import * as React from "react";
import * as axios from 'axios'
import {
    Route,
} from 'react-router-dom';
import config from '../config'

import {
    required,
    SimpleForm,
    useNotify,
    useRefresh,
    useRedirect,
    TextInput,
    NumberInput,
    SelectInput,
    DateInput,
    SaveButton,
    Toolbar,
    ReferenceInput
} from 'react-admin'

import Swal from 'sweetalert2'

import { CustomDateTimeInput } from './components'

const validateRequired = required()

const SaveToolbar = (props) => {
    return (
        <Toolbar {...props} >
            <SaveButton
                label="生成"
                submitOnEnter={true}
            />
        </Toolbar>
    )
}

const Foo = () => {
    const notify = useNotify();
    const refresh = useRefresh();
    const redirect = useRedirect();
    const onSave = async (data, redirectUrl) => {
        Swal.fire({
            title: '正在生成',
        })
        console.log(data, redirectUrl)
        let res = await axios.post(config.base_url + '/api/qrcode/batch_generate', data)
        console.log(res)
        Swal.fire({
            title: '生成成功',
            timer: 1500,
        })
        redirect(redirectUrl)
        refresh()
    }
    return (
        <div>
            <SimpleForm toolbar={<SaveToolbar />} save={onSave} redirect="/qrCode">
                <NumberInput label="二维码数量" source="number" validate={validateRequired} />
                <ReferenceInput label="耗材类型" source="consumableType" reference="consumableType">
                    <SelectInput optionText="type" />
                </ReferenceInput>
                <TextInput label="批次" source="batch"></TextInput>
                {/* <NumberInput label="耗材量" source="amount" validate={validateRequired} /> */}
                <CustomDateTimeInput label="有效日期" source="expireDate" ></CustomDateTimeInput>
            </SimpleForm>
        </div>
    )
}

const Bar = () => (
    <div>
        Bar
    </div>
)

const Baz = () => (
    <div>
        Baz
    </div>
)

export default [
    <Route exact key="foo" path="/foo" component={Foo} />,
    <Route exact key="bar" path="/bar" component={Bar} />,
    <Route exact key="baz" path="/baz" component={Baz} noLayout />,
];